package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"user/model/mysql"

	"user/userrpc/internal/svc"
	"user/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserPerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserPerfectLogic {
	return &UserPerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserPerfectLogic) UserPerfect(in *userrpc.UserPerfectRequest) (*userrpc.UserPerfectResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	mysql.DB.Debug().Where("id = ?", in.Id).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("用户信息错误")
	}
	update := mysql.User{
		Model:       gorm.Model{ID: user.ID},
		Age:         int(in.Age),
		Addr:        in.Addr,
		Email:       in.Email,
		HeaderImage: in.HeaderImage,
	}
	err := mysql.DB.Debug().Updates(&update).Error
	if err != nil {
		return nil, fmt.Errorf("完善用户信息成功")
	}
	return &userrpc.UserPerfectResponse{Id: int64(update.ID)}, nil
}
